package cn.szzsi.business.controller.admin;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.core.controller.BaseController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

public class HomeController extends BaseController{

    private String shopId;

    public void getMemberCenter(){
        shopId=getAttr("shopId");
        //2.会员数量统计
        Record member=Db.findFirst("select sum(case when sourceType='1' then 1 else 0 end) as ppMember,sum(case when sourceType='2' then 1 else 0 end) as wxMember,sum(case when sourceType='3' then 1 else 0 end) as qzMember,sum(case when sourceType='4' then 1 else 0 end) as qqMember from sys_member where shopId=?",shopId);
        setAttr("ppMember",member.get("ppMember"));
        setAttr("wxMember",member.get("wxMember"));
        setAttr("qzMember",member.get("qzMember"));
        setAttr("qqMember",member.get("qqMember"));
        render(CommonUtil.TEMPLATE_PATH+"/business/home/memberHome.html");
    }


    public void getMemberData(){
        shopId=getAttr("shopId");
        JSONObject jsonObject=new JSONObject();
        //1.性别统计
        Record sex= Db.findFirst("select IFNULL(sum(case when sex='1' then 1 else 0 end),0) as boy,IFNULL(sum(case when sex='2' then 1 else 0 end),0) as girl,IFNULL(sum(case when sex='0' then 1 else 0 end),0) as wz from sys_member where shopId=?",shopId);
        Long boy=sex.getLong("boy");
        Long girl=sex.getLong("girl");
        Long wz=sex.getLong("wz");

        JSONArray jsonSex=new JSONArray();
        for(int i=0;i<=2;i++){
            JSONObject jsonSexValue=new JSONObject();
            if(i==0){
                jsonSexValue.put("value",boy);
                jsonSexValue.put("name","男");
            }else if(i==1){
                jsonSexValue.put("value",girl);
                jsonSexValue.put("name","女");
            }else{
                jsonSexValue.put("value",wz);
                jsonSexValue.put("name","未知");
            }
            jsonSex.add(jsonSexValue);
        }
        //System.out.println("sexData------------"+sexData);
        jsonObject.put("sexData",jsonSex);
        //2.会员分布统计
        List<Record> area=Db.find("select m._province,a.longitude,a.latitude,m.amount from sys_location_auxiliary a RIGHT JOIN(select replace(m.province,'省','')_province,count(*) amount from sys_member m where shopId=? and (city is not null and city!='') group by _province)as m ON a.province=m._province",shopId);
        JSONObject jsonArea=new JSONObject();
        for(Record record:area){
            JSONArray arrayGinwave=new JSONArray();
            arrayGinwave.add(record.get("longitude"));
            arrayGinwave.add(record.get("latitude"));
            //
            JSONObject jsonProvince=new JSONObject();
            jsonProvince.put("value",record.get("amount"));
            jsonProvince.put("ginwave",arrayGinwave);
            jsonArea.put(record.getStr("_province"),jsonProvince);

        }
        jsonObject.put("province",jsonArea);
        renderJson(jsonObject);
    }
}
